package com.mmall.pay.controller;

import com.mmall.common.base.ServerResponse;
import com.mmall.pay.enums.PayPlatformTypeEnum;
import com.mmall.pay.exception.MmallPayException;
import com.mmall.pay.param.CloseOrderParam;
import com.mmall.pay.param.CreateOrderParam;
import com.mmall.pay.param.QueryOrderParam;
import com.mmall.pay.param.RefundParam;
import com.mmall.pay.param.RefundQueryParam;
import com.mmall.pay.service.PayService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * @author gg
 * @version PayController.java, v 0.1 2019-05-27 17:07 gg
 */
@Slf4j
@RestController
@RequestMapping("/pay/")
public class PayController {

    @Resource
    private PayService alipayService;

    @Resource
    private PayService wechatPayService;


    /**
     * 查询订单
     *
     * @param queryOrderParam QueryOrderParam
     */
    @ApiOperation(value = "查询订单")
    @PostMapping("queryOrder")
    public ServerResponse queryOrder(@RequestBody @Valid QueryOrderParam queryOrderParam) throws MmallPayException {
        PayService payClient = initClient(queryOrderParam.getPlatFormType());
        return ServerResponse.createBySuccess(payClient.queryOrder(queryOrderParam));
    }

    /**
     * 调用统一下单接口，并组装生成支付所需参数对象.
     *
     */
    @ApiOperation(value = "统一下单，并组装所需支付参数")
    @PostMapping("createOrder")
    public ServerResponse createOrder(@RequestBody @Valid CreateOrderParam createOrderParam) throws MmallPayException {
        PayService payClient = initClient(createOrderParam.getPlatFormType());
        return ServerResponse.createBySuccess(payClient.createOrder(createOrderParam));
    }

    /**
     * 申请退款
     *
     * @param refundParam RefundParam
     * @return 退款操作结果
     */
    @ApiOperation(value = "退款")
    @PostMapping("refund")
    public ServerResponse refund(@RequestBody @Valid RefundParam refundParam) throws MmallPayException {
        PayService payClient = initClient(refundParam.getPlatFormType());
        return ServerResponse.createBySuccess(payClient.refund(refundParam));
    }

    /**
     * 微信支付-查询退款
     * 以下四个参数四选一
     *
     * @return 退款信息
     */
    @ApiOperation(value = "退款查询")
    @PostMapping("refundQuery")
    public ServerResponse refundQuery(@RequestBody @Valid RefundQueryParam refundQueryParam) throws MmallPayException {
        PayService payClient = initClient(refundQueryParam.getPlatFormType());
        return ServerResponse.createBySuccess(payClient.refundQuery(refundQueryParam));
    }

    /**
     * 关闭订单
     * 应用场景
     * 以下情况需要调用关单接口：
     * 1. 商户订单支付失败需要生成新单号重新发起支付，要对原订单号调用关单，避免重复支付；
     * 2. 系统下单后，用户支付超时，系统退出不再受理，避免用户继续，请调用关单接口。
     * 注意：订单生成后不能马上调用关单接口，最短调用时间间隔为5分钟。
     * 接口地址：https://api.mch.weixin.qq.com/pay/closeorder
     *
     * @param closeOrderParam CloseOrderParam
     */
    @ApiOperation(value = "关闭订单")
    @PostMapping("closeOrder")
    public ServerResponse closeOrder(@RequestBody @Valid CloseOrderParam closeOrderParam) throws MmallPayException {
        PayService payClient = initClient(closeOrderParam.getPlatFormType());
        return ServerResponse.createBySuccess(payClient.closeOrder(closeOrderParam));
    }


    private PayService initClient(Integer platFormType) {
        PayPlatformTypeEnum payPlatformTypeEnum = PayPlatformTypeEnum.getByValue(platFormType);
        if (payPlatformTypeEnum == null) {
            throw new RuntimeException("该支付方式不支持");
        }
        switch (payPlatformTypeEnum) {
            case WE_CHAT:
                return wechatPayService;
            case ALI_PAY:
                return alipayService;
            default:
                throw new RuntimeException("该支付方式不支持");
        }
    }

}